package com.nbwyctf.helper.task;

import com.nbwyctf.helper.cache.po.BoardStatusUpdate;
import com.nbwyctf.helper.cache.queue.BoardStatusPriorityQueue;
import com.nbwyctf.helper.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;

/**
 * @author chetaifu
 */
@Service
public class BoardTask implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(BoardTask.class);
    @Autowired
    private BoardStatusPriorityQueue boardQueue;

    @PostConstruct
    public void init(){add();}

    @Override
    public void run(String... args){
        while (true) {
            LocalDateTime now = LocalDateTime.now();
            if (!boardQueue.isEmpty() && !now.isBefore(boardQueue.get().getSendTime())){
                Long boardId = boardQueue.get().getId();
                // .. action
                // .. update
                boardQueue.delete(boardId);
            }
        }
    }

    /** 间隔30分钟执行, 防止优先级队列重启后失效，保障一定持久化能力 */
    @Scheduled(cron = "0 0/30 * * * ? ")
    public void add(){
//        list = mapper.selectAll();
//        for(Board board: list){
//            boardQueue.add(new BoardStatusUpdate(board.getId(), board.getPushTime()));
//        }
    }
}
